import hashlib
import requests
import json
from tkinter import *

# 2021-06-14 0:47 by 炸天帮成员
# 继承自_8_iciba_POST_0.py

EntryValue = None
def getFromEntry():
    global EntryValue
    global myscroll
    global user_input_Entry
    global button
    global label_2 
    EntryValue = user_input_Entry.get()
    print('user_input_Entry.get()  = ',user_input_Entry.get())
    if EntryValue != None :
        print('EntryValue  = ',EntryValue,type(EntryValue))
        #-----------------------------------------------------------------------------
        # 以下为_8_iciba_POST_0.py的内容，实质上为md5解密+POST请求。
        # 原理详见_8_iciba_POST_0.py
        user_input = EntryValue
        hash_type = hashlib.md5()
        hash_type.update(bytes('6key_cibaifanyicjbysdlove1'+user_input,encoding='utf-8'))
        sign = hash_type.hexdigest()[0:16]

        url = f"https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_ciba&sign={sign}"
        post_data = {'from':'auto','to':'auto','q':user_input}
        headers = {
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
            'Content-Length':'23',
            'Content-Type':'application/x-www-form-urlencoded',
            'X-Requested-With': 'XMLHttpRequest',
            'Connection':'keep-alive',
            'Host':'ifanyi.iciba.com',
            'sec-ch-ua':'" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
            'Sec-Fetch-Site':'same-site',
            'Sec-Fetch-Mode':'cors',
            'Sec-Fetch-Dest':'empty',
            }
        r=requests.post(url,data=post_data,headers=headers)
        print(r.text)
        json_data = json.loads(r.text)
        print(f'{user_input}的中文翻译为：' + json_data['content']['out'])
        #-----------------------------------------------------------------------
        # 以下为Tk的GUI界面刷新，思路为先删除原先的全局GUI控件，然后再重新生成。
        label_2.destroy()
        label_var_2 = StringVar()
        label_var_2.set(f'“{user_input}”的中文翻译为：' )
        label_2 = Label(frame,textvariable = label_var_2)
        label_2.pack()

        user_input_Entry.destroy()
        myscroll.destroy()
        myscroll = Scrollbar(frame,orient= HORIZONTAL)
        user_input_Entry = Entry(frame,width = 400, xscrollcommand = myscroll.set)
        user_input_Entry.insert(0,json_data['content']['out'])
        user_input_Entry.pack()
        myscroll.pack(fill = X)
        myscroll.config(command = user_input_Entry.xview)

        button.destroy()
        button = Button(frame,text = "确定",command = getFromEntry)
        button.pack()
#---------------------------------------------------------------------------
if __name__ == "__main__":
    # 以下为TK的GUI界面部分
    root = Tk()
    root.geometry('400x400')
    root.title('iciba整句翻译')
    frame = Frame(root)
    frame.pack()

    label_var = StringVar()
    label_var.set('输入你想要查询的英文单词或者句子：')
    label = Label(frame,textvariable = label_var)
    label.pack()
    
    label_var_2 = StringVar()
    label_var_2.set(f'中文翻译为：' )
    label_2 = Label(frame,textvariable = label_var_2)
    label_2.pack()

    myscroll = Scrollbar(frame,orient= HORIZONTAL)  # 带X轴方向滚动条的输入框
    user_input_Entry = Entry(frame,width = 400, xscrollcommand = myscroll.set)
    user_input_Entry.pack()
    myscroll.pack(fill = X)
    myscroll.config(command = user_input_Entry.xview)

    button = Button(frame,text = "确定",command = getFromEntry) # 当按钮按下后，执行getFromEntry()函数中的iciba爬取内容
    button.pack()

    root.mainloop()
